package com.fenda.jetpacknavigation.roomdata;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.fenda.jetpacknavigation.R;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.CompositeDisposable;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {
    private List<User> lists = new ArrayList<>();
    private onCheck onCheck;


    public void setData(List<User> users, onCheck onCheck) {
        this.lists = users;
        this.onCheck = onCheck;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.room_item1, parent, false);
        final MyHolder holder = new MyHolder(view);
        holder.aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                User user = (User) holder.itemView.getTag();
                if (isChecked) {
                    holder.tvName2.setVisibility(View.GONE);
                    user.setFlag(true);
                    onCheck.setFlag(user);
                } else {
                    holder.tvName2.setVisibility(View.VISIBLE);
                    user.setFlag(false);
                    onCheck.setFlag(user);
                }
            }
        });

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        User user = lists.get(position);
        user.setPosition(position);
        holder.itemView.setTag(user);
        holder.tvNum.setText(String.valueOf(user.getId()));
        holder.tvName1.setText(user.getFirstName());
        holder.tvName2.setText(user.getLastName());

        if (user.getFlag()) {
            holder.aSwitch.setChecked(true);
            holder.tvName2.setVisibility(View.GONE);
        } else {
            holder.aSwitch.setChecked(false);
            holder.tvName2.setVisibility(View.VISIBLE);
        }
    }

    public interface onCheck {
        void setFlag(User user);
    }

    @Override
    public int getItemCount() {
        return lists.size();
    }

    static class MyHolder extends RecyclerView.ViewHolder {

        private TextView tvNum, tvName1, tvName2;
        private Switch aSwitch;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            tvNum = itemView.findViewById(R.id.mun);
            tvName1 = itemView.findViewById(R.id.textViewfisetname);
            tvName2 = itemView.findViewById(R.id.textViewlastname);
            aSwitch = itemView.findViewById(R.id.switch1);
        }
    }
}
